home *** CD-ROM | disk | FTP | other *** search
/ Graphics Plus / Graphics Plus.iso / general / plotting / contour / contour.lha / Contour / plothp.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-08-28  |  7.3 KB  |  248 lines

  1. /*** plothp.c - plot out in hp format ***/
  2.  
  3. #include <stdio.h>
  4. #include <strings.h>
  5. #include <math.h>
  6. #include "common.h"
  7. #include "contour.h"
  8. #include "plot.h"
  9.  
  10. #define HX_DIM   500
  11. #define HY_DIM   400
  12. #define HBDR_DIM  50 
  13. double  Hxmin, Hxmax, Hymin, Hymax;
  14.  
  15. /* draw the axes */
  16. axeshp()
  17. {
  18.     char   *sprintf();
  19.     extern char    xlabel[MAXCHAR];
  20.     extern char    ylabel[MAXCHAR];
  21.     extern char    toplabel[MAXCHAR];
  22.     extern int     grid,equalscale,xticks,yticks;
  23.     extern double  xmin, ymin, xmax, ymax;
  24.  
  25.     double  xdx,ydy;
  26.     double  xintv,yintv,xtmp,ytmp,xwid,ywid,ratio;
  27.     double  vallbl;
  28.     int     i;
  29.     char    label[10];
  30.  
  31.     /* Initialize */
  32.     xdx = (xmax-xmin)/xticks;
  33.     ydy = (ymax-ymin)/yticks;
  34.  
  35.     Hxmin = HBDR_DIM;
  36.     Hymin = HBDR_DIM;
  37.     xwid  = HX_DIM - 2*HBDR_DIM;
  38.     ywid  = HY_DIM - 2*HBDR_DIM;
  39.     Hxmax = Hxmin + xwid;
  40.     Hymax = Hymin + ywid; 
  41.     if (equalscale == ON) {
  42.        /* max xwid = 400. max ywid = 300 */
  43.        ratio = (ymax-ymin)/(xmax-xmin);
  44.        if (ratio <= (ywid/xwid) ) {
  45.           Hxmax = Hxmin + xwid;
  46.           Hymax = Hymin + ratio*xwid;
  47.        } else {
  48.           Hymax = Hymin + ywid;
  49.           Hxmax = Hxmin + ywid/ratio;
  50.        }
  51.     }
  52.     xintv = (Hxmax-Hxmin)/xticks;
  53.     yintv = (Hymax-Hymin)/yticks;
  54.  
  55.     /* Turn on graphics display, set line type = 1 */
  56.     printf("%c*dfz%c*dcz%c*m1b1q3x\n",esc,esc,esc);
  57.  
  58.     /* Define a grid pattern */
  59.     printf("%c*m 8 1 cz\n",esc);
  60.  
  61.     /* define the axes */
  62.     printf("%c*paf %6f %6f %6f %6f z\n",esc,Hxmin,Hymin,Hxmin,Hymax);
  63.     printf("%c*paf %6f %6f %6f %6f z\n",esc,Hxmin,Hymax,Hxmax,Hymax);
  64.     printf("%c*paf %6f %6f %6f %6f z\n",esc,Hxmax,Hymax,Hxmax,Hymin);
  65.     printf("%c*paf %6f %6f %6f %6f z\n",esc,Hxmax,Hymin,Hxmin,Hymin);
  66.     printf("%c*paf %6f %6f %6f %6f z\n",esc,Hxmin-1,Hymin-1,Hxmin-1,Hymax+1);
  67.     printf("%c*paf %6f %6f %6f %6f z\n",esc,Hxmin-1,Hymax+1,Hxmax+1,Hymax+1);
  68.     printf("%c*paf %6f %6f %6f %6f z\n",esc,Hxmax+1,Hymax+1,Hxmax+1,Hymin-1);
  69.     printf("%c*paf %6f %6f %6f %6f z\n",esc,Hxmax+1,Hymin-1,Hxmin-1,Hymin-1);
  70.  
  71.     /* Set the options for the labels */
  72.     printf("%c*m 1n4q7x\n",esc);
  73.  
  74.     /* Top Label */
  75.     printf("%c*dsk %6f %6f oz\n",esc,0.5*(Hxmax+Hxmin),Hymax+10);
  76.     printf("%c*dS%s\n",esc,toplabel);
  77.  
  78.     /* Set the options for the labels */
  79.     printf("%c*m 1n6q7x\n",esc);
  80.  
  81.     /* X-axis Label */
  82.     printf("%c*dsk %6f %6f oz\n",esc,0.5*(Hxmax+Hxmin),Hymin-20);
  83.     printf("%c*dS%s\n",esc,xlabel);
  84.  
  85.     /* Set the options for the labels */
  86.     printf("%c*m 2n4q7x\n",esc);
  87.  
  88.     /* Y-axis Label */
  89.     printf("%c*dsk %6f %6f oz\n",esc,Hxmin-40,0.5*(Hymin+Hymax));
  90.     printf("%c*dS%s\n",esc,ylabel);
  91.  
  92.     /* Set the options for the labels */
  93.     printf("%c*m 1n6q3x\n",esc);
  94.  
  95.     /* side labels */
  96.     sprintf(label,"%s","MINIMUM");
  97.     printf("%c*paf %6f %6f z\n",esc,Hxmax+40,Hymax-20);
  98.     printf("%c*l%s\n",esc,label);
  99.     sprintf(label,"%.2f",zmin);
  100.     printf("%c*paf %6f %6f z\n",esc,Hxmax+40,Hymax-40);
  101.     printf("%c*l%s\n",esc,label);
  102.     sprintf(label,"%s","MAXIMUM");
  103.     printf("%c*paf %6f %6f z\n",esc,Hxmax+40,Hymin+40);
  104.     printf("%c*l%s\n",esc,label);
  105.     sprintf(label,"%.2f",zmax);
  106.     printf("%c*paf %6f %6f z\n",esc,Hxmax+40,Hymin+20);
  107.     printf("%c*l%s\n",esc,label);
  108.  
  109.     /* Set the options for the labels */
  110.     printf("%c*m 1n1q3x\n",esc);
  111.  
  112.     /* Tick Marks on the X-axis */
  113.     for (i=0; i<=xticks; i++) {
  114.        xtmp = Hxmin + xintv*i;
  115.        ytmp = Hymin;
  116.        printf("%c*paf %6f %6f %6f %6f z\n",esc,xtmp,ytmp,xtmp,ytmp+3);
  117.  
  118.        vallbl = xmin + i*xdx;
  119.        sprintf(label,"%6.2f",vallbl);
  120.        printf("%c*paf %6f %6f z\n",esc,xtmp-35,ytmp-15);
  121.        printf("%c*l%s\n",esc,label);
  122.  
  123.        ytmp = Hymax;
  124.        printf("%c*paf %6f %6f %6f %6f z\n",esc,xtmp,ytmp-3,xtmp,ytmp);
  125.  
  126.        if (grid == ON) {
  127.           linetyphp(2);
  128.           printf("%c*paf %6f %6f %6f %6f z\n",esc,xtmp,Hymin+3,xtmp,Hymax-3);
  129.           linetyphp(1);
  130.        }
  131.     }
  132.  
  133.     /* Tick Marks on the Y-axis */
  134.     for (i=0; i<=yticks; i++) {
  135.        ytmp = Hymin + yintv*i;
  136.        xtmp = Hxmin;
  137.        printf("%c*paf %6f %6f %6f %6f z\n",esc,xtmp,ytmp,xtmp+3,ytmp);
  138.  
  139.        vallbl = ymin + i*ydy;
  140.        sprintf(label,"%6.2f",vallbl);
  141.        printf("%c*paf %6f %6f z\n",esc,xtmp-55,ytmp);
  142.        printf("%c*l%s\n",esc,label);
  143.  
  144.        xtmp = Hxmax;
  145.        printf("%c*paf %6f %6f %6f %6f z\n",esc,xtmp-3,ytmp,xtmp,ytmp);
  146.  
  147.        if (grid == ON) {
  148.           linetyphp(2);
  149.           printf("%c*paf %6f %6f %6f %6f z\n",esc,Hxmin+3,ytmp,Hxmax-3,ytmp);
  150.           linetyphp(1);
  151.        }
  152.     }
  153. }
  154.  
  155. /* plot the curves */
  156. plothp()
  157. {
  158.    char   *sprintf();
  159.    extern int     linetypes;
  160.    extern double  xmin, xmax, ymin, ymax;
  161.    extern plotptr plot_listhead;
  162.  
  163.    plotptr  P;
  164.    nodeptr  N;
  165.    char     text[10];
  166.    double   x,y,xm,ym,idx,jdy,oldlevel;
  167.    int      linepat, npts, j, m, kl=1;
  168.  
  169.    idx = (Hxmax-Hxmin)/(xmax-xmin);
  170.    jdy = (Hymax-Hymin)/(ymax-ymin);
  171.  
  172.    if (plot_listhead!=NULL) oldlevel = plot_listhead->level;
  173.    for (P=plot_listhead; P!=NULL; P=P->next) {
  174.       /* first count the number of points in the plot */
  175.       npts=0;
  176.       for (N=P->nodehead; N!=NULL; N=N->next) npts++;
  177.       m = (int)(0.5*npts)-1;
  178.  
  179.       if (npts>0) {
  180.          /* plot the points */
  181.          if (oldlevel != P->level) kl++;
  182.          linepat = linetyphp(kl);
  183.          j = 0;
  184.          printf("%c*paf \n",esc);     /* new line */
  185.          for (N=P->nodehead; N!=NULL; N=N->next) {
  186.             x = (N->pt.x - xmin)*idx + Hxmin;
  187.             y = (N->pt.y - ymin)*jdy + Hymin;
  188.             printf("%6f %6f \n",x,y);
  189.             if (j==m) { xm=x; ym=y; }
  190.             j++;
  191.          }
  192.          printf(" z\n");
  193.  
  194.          /* label the contours */
  195.          if ((linepat==0||linepat==1) && (m>=0 && m<npts)) {
  196.             /* Set the options for the labels */
  197.             printf("%c*m 1n6q3x\n",esc);
  198.             sprintf(text,"%.2f",P->level);
  199.             printf("%c*paf %6f %6f z\n",esc,xm,ym);
  200.             printf("%c*l%s\n",esc,text);
  201.          }
  202.       }
  203.       oldlevel = P->level;
  204.    }
  205. }
  206.  
  207. /* change the linetype */
  208. int linetyphp(line)
  209. int line; 
  210. {
  211.    extern int linetypes;
  212.    int linepat, newline, newcolor;
  213.  
  214.    if      (linetypes <= 2) line = line % linetypes;
  215.    else if (linetypes == 3) line = line %4;
  216.  
  217.    switch (line) {
  218.    case 0 : newline = 1; newcolor = 7; linepat = 0; break;
  219.    case 1 : newline = 7; newcolor = 6; linepat = 2; break;
  220.    case 2 : newline = 5; newcolor = 7; linepat = 1; break;
  221.    case 3 : newline = 7; newcolor = 6; linepat = 2; break;
  222.    default: newline = 1; newcolor = 7; linepat = 0; break;
  223.    }
  224.  
  225.    printf("%c*m%db%dxz\n",esc,newline,newcolor);
  226.  
  227.    return(linepat);
  228.    /* newline =  1 : solid line             color = 0 : black      */
  229.    /* newline =  4 : dot-dash               color = 1 : red        */
  230.    /* newline =  5 : long dash              color = 2 : green      */
  231.    /* newline =  6 : short dash             color = 3 : yellow     */
  232.    /* newline =  7 : dots                   color = 4 : dark blue  */
  233.    /* newline =  8 : dot-dash               color = 5 : purple     */
  234.    /* newline =  9 : 3-dash                 color = 6 : light blue */
  235.    /* newline = 10 : 3-dash                 color = 7 : white      */
  236.    /* newline = 11 : point plot                                    */
  237. }
  238.  
  239. /* end procedures - reset the terminal */
  240. endgrhp()
  241. {
  242.    /* Turn off graphics text mode */
  243.    printf("%c*dt\n",esc);
  244.  
  245.    printf("%c*delt %c*m1b7xZ\n",esc,esc);
  246. }
  247.  
  248.